package 回溯;

import java.util.LinkedList;
import java.util.List;

//47 全排列二
public class Solution8 {
    public List<List<Integer>> permuteUnique(int[] nums) {
        dfs(new LinkedList(), new int[nums.length], nums);
        return res;
    }

    List<List<Integer>> res = new LinkedList();
    private void dfs(List<Integer> path, int[] vst, int[] nums) {
        if (path.size() == nums.length) {
            res.add(new LinkedList(path));
            return;
        }
        int[] usd = new int[21];
        for (int i = 0 ; i < nums.length ; i++) {
            if (vst[i] == 0 && usd[nums[i] + 10] == 0) {
                usd[nums[i] + 10] = 1;
                vst[i] = 1;
                path.add(nums[i]);
                dfs(path, vst, nums);
                path.remove(path.size() - 1);
                vst[i] = 0;
            }
        }
    }
}
